package org.jeecg.modules.simulator.loader;

import lombok.Setter;
import org.jeecg.modules.simulator.factory.LoadTrainTypeService;
import org.jeecg.modules.simulator.factory.park.ParkSaverService;
import org.jeecg.modules.simulator.factory.SimLineService;
import org.jeecg.modules.simulator.factory.park.SimParkerService;
import org.jeecg.modules.simulator.factory.station.db.SimStationService;
import org.jeecg.modules.simulator.factory.TracksDbLoader;
import org.jeecg.modules.simulator.factory.orbit.TracksFactory;
import org.jeecg.modules.simulator.factory.station.json.StationFactory;
import org.jeecg.modules.simulator.factory.station.json.Stationbs;
import org.jeecg.modules.simulator.factory.train.plan.ParkJsonSaverRead;
import org.jeecg.modules.simulator.factory.train.plan.ParkTrainLoader;
import org.jeecg.modules.simulator.factory.train.plan.Train2Parker;
import org.jeecg.modules.simulator.factory.train.plan.TrainPhysicsDataLoader;
import org.jeecg.modules.simulator.jsonot.RouteStation;
import org.jeecg.modules.simulator.jsonot.T80RoutOD;
import org.jeecg.modules.simulator.line.LineFactory;
import org.jeecg.modules.simulator.model.line.Line;
import org.jeecg.modules.simulator.model.line.park.Parker;
import org.jeecg.modules.simulator.model.line.park.load.ParkJsonFactory;
import org.jeecg.modules.simulator.model.line.park.load.ParkerLoader;
import org.jeecg.modules.simulator.model.line.station.station.Station;
import org.jeecg.modules.simulator.model.line.station.station.TransferStation;
import org.jeecg.modules.simulator.model.orbit.OrbitPoint;
import org.jeecg.modules.simulator.model.train.TrainPhysics;
import org.jeecg.modules.simulator.service.T80RouteHisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @program: simulator-b20240918
 * @description:
 * @author: renzhg
 * @create: 2024-10-04 11:24
 **/
@Component
public class BasicDataFactory {



    @Setter
    Boolean isDb = false;


//    @Autowired
//    T80RouteHisService t80RouteHisService;
    ParkTrainLoader parkJsonSaverRead = new ParkJsonSaverRead();

    @Autowired
    ParkSaverService parkSaverService;

    @Autowired
    LoadTrainTypeService loadTrainTypeService;


    //列车类型
    TrainPhysicsDataLoader trainPhysicsDataLoader = new TrainPhysicsDataLoader();

    //轨道数据生成---用的json数据
    TracksFactory tracksFactory = new TracksFactory();
    //轨道数据生成-----用的数据库数据
    TracksDbLoader tracksDbLoader = new TracksDbLoader();
    //---数据库
    @Autowired
    SimParkerService packFactory1;
    //---json
    ParkerLoader packFactory2 = new ParkJsonFactory();

    //---json
    StationFactory stationFactory = new StationFactory();
    //---数据库
    @Autowired
    SimStationService simStationService;


    //---json
    LineFactory lineFactory = new LineFactory();
    //---数据库
    @Autowired
    SimLineService simLineService;

//    @Getter
//    private Map<String, Line> lineMap = new HashMap<>();
//    @Getter
//    private Map<String, List<Station>> stationMap = new HashMap<>();//按线路分类
//    @Getter
//    private Map<String, TransferStation> transferStationMap = new HashMap<>();
//    @Getter
//    private Map<String, List<Parker>> parkerMap = new HashMap<>();//按线路分类
//    @Getter
//    Map<String, TrainPhysics> trainPhysicsMap = new HashMap<>();
//    @Getter
//    List<Train2Parker> tplist = new ArrayList<>();

//    @Getter
//    Map<String, List<OrbitPoint>> orbitPointpMap = new HashMap<>();


    public GlobalSceneBaseData loadBasicData() {
        GlobalSceneBaseData globalSceneBaseData = new GlobalSceneBaseData();
        Map<String, Line> lineMap = loadLine();
        globalSceneBaseData.setLineMap(lineMap);
        Map<String, List<Station>> stationListMap = loadStation();
        globalSceneBaseData.setLineMap(lineMap);
        globalSceneBaseData.setLineStationMap(stationListMap);
//        t80RouteHisService.load();
//        Map<String, List<RouteStation>> t80RouteHisMap=t80RouteHisService.getT80RouteHisMap();

//        Map<String, List<T80RoutOD>> t80ODRouteMap=t80RouteHisService.getT80ODRouteMap();

        Map<String, TransferStation> transferStationMap = loadTransferStation();
        globalSceneBaseData.setTransferStationMap(transferStationMap);

        List<Station> stationList = stationListMap.values().stream()
                .flatMap(List::stream)
                .collect(Collectors.toList());
        Map<String, List<OrbitPoint>> orbitPointListMap = loadObits(stationList);
        globalSceneBaseData.setOrbitPointListMap(orbitPointListMap);
        Map<String, List<Parker>> parkerListMap = loadPark();
        globalSceneBaseData.setParkerMap(parkerListMap);
        loadAssistance();
        loadPowerSupplySection();
        Map<String, TrainPhysics> TrainPhysicsMap = loadTrainType();
        globalSceneBaseData.setTrainPhysicsMap(TrainPhysicsMap);
        List<Train2Parker> train2Parkers = loadPark2Train();
        globalSceneBaseData.setTrain2Parkers(train2Parkers);
        globalSceneBaseData.setTplist(train2Parkers);
//        globalSceneBaseData.setT80RouteHisMap(t80RouteHisMap);


//        globalSceneBaseData.setT80ODRouteMap(t80ODRouteMap);
        return globalSceneBaseData;

    }

    private Map<String, TransferStation> loadTransferStation() {

        if (isDb) {
            return simStationService.getStationTransferMap();
        } else {
            return stationFactory.getStationTransferMap();
        }


    }


    public void loadAssistance() {

    }

    public void loadPowerSupplySection() {

    }

    public List<Train2Parker> loadPark2Train() {
        if(isDb){
            parkSaverService.saveParkSaver();
            return parkSaverService.getTrain2Parkers();
        }else{
            parkJsonSaverRead.dataLoad();
            return parkJsonSaverRead.getTplist();
        }



    }


    public Map<String, Line> loadLine() {

        if (isDb) {
            simLineService.loadData();
            return simLineService.getLineMap();
        } else {
            lineFactory.loadData();
            return lineFactory.getLineMap();
        }

    }

    /**
     * @description: 加载站数据
     * @author: renzhg
     * @date: 2024/8/30 14:08
     **/
    public Map<String, List<Station>> loadStation() {

        if (isDb) {
            simStationService.loadData();
            return simStationService.getStationSimpleMap();
        } else {
            stationFactory.loadData();
            return stationFactory.getStationSimpleMap();
        }

    }

    public Map<String, List<Parker>> loadPark() {
        if (isDb) {
            packFactory1.loadParkData();
            return packFactory1.getParkerMap();
        } else {
            packFactory2.loadParkData();
            return packFactory2.getParkerMap();
        }

    }

    public Map<String, TrainPhysics> loadTrainType() {
        if (isDb) {
            loadTrainTypeService.initTrainPhyMap();
            return loadTrainTypeService.getTrainPhysicsMap();
        } else {
            trainPhysicsDataLoader.loadData();
            return trainPhysicsDataLoader.getTrainPhysicsMap();
        }

//        trainFactory.load();
//        trainPhysicsMap = trainFactory.getTrainPhysicsMap();
//        noAssignMetroTrainMap = trainFactory.getNoAssignMetroTrainMap();

    }


    private Map<String, List<OrbitPoint>> loadObitByDb(List<Station> stationList) {

        TracksDbLoader tracksDbLoader = new TracksDbLoader();
        tracksDbLoader.setStationList(stationList);
        tracksDbLoader.initTracksDb();
        List<OrbitPoint> orbitPointList = tracksDbLoader.getOrbitPath();
        return orbitPointList.stream().collect(Collectors.groupingBy(OrbitPoint::getLineId));
    }


    private Map<String, List<OrbitPoint>> loadObits(List<Station> stationList) {

        if (isDb) {
            return loadObitByDb(stationList);
        } else {
            List<Stationbs> stationbsList = stationFactory.getStationbsList();
            tracksFactory.setStationbsList(stationbsList);
            tracksFactory.initReadOrbitPath();

            List<OrbitPoint> orbitPointList = tracksFactory.getOrbitPath();
            return orbitPointList.stream().collect(Collectors.groupingBy(OrbitPoint::getLineId));
        }


    }
}
